﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolrNet.Attributes;
using ProtoBuf;

namespace NewsMine.DomainObjects
{
    [Serializable]
    [ProtoContract]
    public class FeedItem
    {
        public FeedItem()
        {
            this.Tags = new List<string>();
        }

        [ProtoMember(1)]
        [SolrField("author")]
        public string Author { get; set; }

        [ProtoMember(2)]
        [SolrField("language")]
        public string Language { get; set; }

        [ProtoMember(3)]
        [SolrField("body")]
        public string Body { get; set; }

        [ProtoMember(4)]
        [SolrField("cat")]
        public string category { get; set; }

        [ProtoMember(5)]
        [SolrField("desc")]
        public string Description { get; set; }

        [ProtoMember(6)]
        [SolrField("domain")]
        public string Domain { get; set; }

        [ProtoMember(7)]
        public string ID { get; set; }

        [ProtoMember(8)]
        [SolrField("imgurl")]
        public string ImageUrl { get; set; }

        [ProtoMember(9)]
        public List<string> AnalysedTags { get; set; }

        string _anlyzedTag = null;
        public string AnalyzedTag
        {
            get
            {
                if (_anlyzedTag==null)
                {
                    if (AnalysedTags != null && AnalysedTags.Count >0)
                    {
                        _anlyzedTag = AnalysedTags.Where(t => !string.IsNullOrEmpty(t)).FirstOrDefault<string>();

                        if (_anlyzedTag==null)
                            _anlyzedTag = string.Empty;
                    }
                    else
                        _anlyzedTag = string.Empty;
                }

                return _anlyzedTag;
            }
        }

        [ProtoMember(10)]
        [SolrField("keyword")]
        public string Keyworkds { get; set; }

        [ProtoMember(11)]
        [SolrField("link")]
        public string Link { get; set; }

        [ProtoMember(12)]
        [SolrField("pubdate")]
        public DateTime PubDate { get; set; }

        [ProtoMember(13)]
        [SolrField("rank")]
        public float Rank { get; set; }

        [ProtoMember(14)]
        [SolrField("refurl")]
        public string RefUrl { get; set; }

        [ProtoMember(15)]
        [SolrField("tag")]
        public List<string> Tags { get; set; }

        [ProtoMember(16)]
        [SolrField("thumb")]
        public string ThumbImageUrl { get; set; }

        [ProtoMember(17)]
        [SolrField("title")]
        public string Title { get; set; }

        [ProtoMember(18)]
        [SolrField("updatedtime")]
        public DateTime UpdatedDateTime { get; set; }

        [ProtoMember(19)]
        [SolrField("touchtime")]
        public DateTime LastTouchTime { get; set; }

        [ProtoMember(20)]
        public int ImageWidth { get; set; }
        [ProtoMember(21)]
        public int ImageHeight { get; set; }

        [ProtoMember(22)]
        public bool IsFeedSourced { get; set; }
        
        [ProtoMember(23)]
        public List<string>  CategoriesFromFeed { get; set; }

        public void Sanitize()
        {

            if (this.Tags != null && this.Tags.Count > 0)
            {
                this.Tags = (from tg in this.Tags
                             where !string.IsNullOrEmpty(tg)
                             select tg.ToLower()).Distinct<string>().ToList();
            }

        }

    }


    [Serializable]
    [ProtoContract]
    public struct categoryRank
    {
        [ProtoMember(1)]
        public string Name { get; set; }
        [ProtoMember(2)]
        public double Count { get; set; }
        [ProtoMember(3)]
        public double GlobalPercent { get; set; }
    }
}
